/*
 * @Author: 刘庆州 
 * @Date: 2022-01-08 17:11:07 
 * @Last Modified by: 刘庆州
 * @Last Modified time: 2022-03-17 10:19:46
 */


import HttpUtils from '../utils/HttpUtil'

import UserManager from '../common/UserManager'
import Constants from '../common/Constants'
/**
 * 获取appCategory
 * @param params {userInfo}
 * @returns {Promise<any | never>}
 */
 const  getAppCategorysApi = async () => {

   var userInfo = await UserManager.getUserInfo()
  const ticket = await UserManager.getTicket()

  console.log("userInfo is:", userInfo);
  const contextParam = {
    InvOrgId: userInfo.InvOrg,
    Ticket: ticket === null ? '' : ticket,
  };
  const parameters = [{
    'Value': userInfo.UserId,
  }];

  const params = {
    ApiType: 'AppsMenuController',
    Context: contextParam,
    Method: 'GetAppCategorys',
    Parameters: parameters,

  };

  

    console.log(params);
    return HttpUtils.POST('/Server.svc/api/invoke', params)
      .then(resp => {
        console.log(resp);
        return Promise.resolve(resp);
      })
      .catch(resp => {
        Toast.showToast(resp);
        console.log(resp);
        return Promise.reject(resp);
      });
  };


  /**
 * 获取app列表
 * @param params {userInfo}
 * @returns {Promise<any | never>}
 */
 const getAppsByCategorysIdApi = async (categoryId) => {

  var userInfo = await UserManager.getUserInfo()
  const ticket = await UserManager.getLongTicket()
  const contextParam = {
    InvOrgId: userInfo.InvOrg,
    Ticket: ticket === null ? '' : ticket,
  };
  const parameters = [{
    'Value': categoryId,
  },{
    'Value': userInfo.UserId,
  }];

  const params = {
    ApiType: 'AppsMenuController',
    Context: contextParam,
    Method: 'GetAppsByCategoryId',
    Parameters: parameters,

  };

  

    console.log(params);
    return HttpUtils.POST('/Server.svc/api/invoke', params)
      .then(resp => {
        return Promise.resolve(resp);
      })
      .catch(resp => {
        Toast.showToast(resp);
        return Promise.reject(resp);
      });
  };


  const getAppByIdApi = (id) => {

    var userInfo = UserManager.getUserInfo()
    const ticket = UserManager.getLongTicket()
    const contextParam = {
      InvOrgId: userInfo.InvOrg,
      Ticket: ticket === null ? '' : ticket,
    };
    const parameters = [{
      'Value': id,
    }];
  
    const params = {
      ApiType: 'AppsMenuController',
      Context: contextParam,
      Method: 'GetAppById',
      Parameters: parameters,
  
    };
  
    
  
      console.log(params);
      return HttpUtils.POST('/Server.svc/api/invoke', params)
        .then(resp => {
          return Promise.resolve(resp);
        })
        .catch(resp => {
          Toast.showToast(resp);
          return Promise.reject(resp);
        });
    };

    const getAppsByIdsApi = (ids) => {
     // ids : "190,200"
       var userInfo = UserManager.getUserInfo()
      const ticket = UserManager.getLongTicket()
      const contextParam = {
        InvOrgId: userInfo.InvOrg,
        Ticket: ticket === null ? '' : ticket,
      };
      const parameters = [{
        'Value': ids,
      }];
    
      const params = {
        ApiType: 'AppsMenuController',
        Context: contextParam,
        Method: 'GetAppsByIds',
        Parameters: parameters,
    
      };
    
      
    
        console.log(params);
        return HttpUtils.POST('/Server.svc/api/invoke', params)
          .then(resp => {
            return Promise.resolve(resp);
          })
          .catch(resp => {
            Toast.showToast(resp);
            return Promise.reject(resp);
          });
      };

  const getInvOrgUserApi = (ids) => {
    // ids : "190,200"
      var userInfo = UserManager.getUserInfo()
      const ticket = UserManager.getLongTicket()
      const contextParam = {
        InvOrgId: userInfo.InvOrg
      };
      const parameters = [{
        'Value': userInfo.UserId,
      }];
    
      const params = {
        ApiType: 'AppsMenuController',
        Context: contextParam,
        Method: 'GetInvOrgUser',
        Parameters: parameters,
    
      };
    
      
    
      console.log(params);
      return HttpUtils.POST('/Server.svc/api/invoke', params)
        .then(resp => {
          return Promise.resolve(resp);
        })
        .catch(resp => {
          Toast.showToast(resp);
          return Promise.reject(resp);
        });
    };

    const changeInvOrgApi = (ids) => {
      // ids : "190,200"
        var userInfo = UserManager.getUserInfo()
        const ticket = UserManager.getLongTicket()
        const contextParam = {
          InvOrgId: userInfo.InvOrg,
          Ticket: ticket === null ? '' : ticket
        };
        const parameters = [{
          'Value': userInfo.UserId,
        },{
          'Value': userInfo.InvOrg,
        }];
      
        const params = {
          ApiType: 'AppsMenuController',
          Context: contextParam,
          Method: 'ChangeInvOrg',
          Parameters: parameters,
      
        };
      
        
      
        console.log(params);
        return HttpUtils.POST('/Server.svc/api/invoke', params)
          .then(resp => {
            return Promise.resolve(resp);
          })
          .catch(resp => {
            Toast.showToast(resp);
            return Promise.reject(resp);
          });
      };


      //http://10.1.8.51/RF_Attachment/AppMenuAttachment/config.json
      // http://10.1.8.51/RF_Attachment/AppMenuAttachment/PluginMain.apk
         
  const getAppVersionConfigApi = ()=>{


    return HttpUtils.GET_FULL(Constants.serverUrl + '/RF_Attachment/AppMenuAttachment/config.json', {})
    .then(resp => {

      return Promise.resolve(resp);
    })
    .catch(resp => {
      Toast.showToast(resp);
      return Promise.reject(resp);
    });

  };

   
 /**
 * 获取信息
 * @param params {userInfo}
 * @returns {Promise<any | never>}
 */
  const getTidInfoAPI =async (tid) => {

    console.log("getTidInfo call")

    var userInfo = await UserManager.getUserInfo()
    const ticket = await UserManager.getLongTicket()
    const contextParam = {
      InvOrgId: userInfo.InvOrg,
      Ticket: ticket === null ? '' : ticket,
    };
    const parameters = [{
      'Value': tid,
    }];
  
    const params = {
      ApiType: 'SiteRegistrationCommonController',
      Context: contextParam,
      Method: 'GetTieInfo',
      Parameters: parameters,
  
    };
  
    
  
      console.log(params);
      return HttpUtils.POST('/Server.svc/api/invoke', params)
        .then(resp => {
          return Promise.resolve(resp);
        })
        .catch(resp => {
          // Toast.showToast(resp);
          return Promise.reject(resp);
        });
    };
   

     /**
 * 绑定RFID
 * @param params {userInfo}
 * @returns {Promise<any | never>}
 */
  const bindRFIDApi =async (tid,rfid) => {

    var userInfo = await UserManager.getUserInfo()
    const ticket = await UserManager.getLongTicket()
    const contextParam = {
      InvOrgId: userInfo.InvOrg,
      Ticket: ticket === null ? '' : ticket,
    };
    const parameters = [{
      'Value': tid,
    },{
        'Value': rfid,
      }];
  
    const params = {
      ApiType: 'SiteRegistrationCommonController',
      Context: contextParam,
      Method: 'BindTieRfid',
      Parameters: parameters,
  
    };
  
    
  
      console.log(params);
      return HttpUtils.POST('/Server.svc/api/invoke', params)
        .then(resp => {
          return Promise.resolve(resp);
        })
        .catch(resp => {
       
          return Promise.reject(resp);
        });
    };

  module.exports = {
    getAppCategorysApi,getAppsByCategorysIdApi, getAppByIdApi, getAppsByIdsApi, getInvOrgUserApi, changeInvOrgApi, getAppVersionConfigApi,getTidInfoAPI,bindRFIDApi

  };
  
